package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.mapper.DbusstatiMapper;
import cn.gson.fengboot.model.pojos.Dbusiness;
import cn.gson.fengboot.model.pojos.Dcount;
import cn.gson.fengboot.model.pojos.DmoneyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class DbusstatiService {
    @Autowired
    DbusstatiMapper dbusstatiMapper;

    @Autowired
    DloginService dloginService;

    /*折线图我的商机本年数据*/
    public List<Map<String,Object>> selectByMonthSize(String getbuspeople) {
        List<Dcount> list = dbusstatiMapper.selectByMonthSize(getbuspeople);
        List<Map<String,Object>> list1 = new ArrayList<>();
        for (Dcount d : list) {
            Map<String,Object> map = new HashMap<>();
            map.put("日期",d.getName());
            map.put("商机数量",d.getValue());
            list1.add(map);
        }
        return list1;
    }


    /*折线图我可查看商机本年数据*/
    public List<Map<String,Object>>  selectByMonthSizemyall(HttpServletRequest request) {
        List<String> getbuspeoplelist = dloginService.getbuspeoplelist(request);
        List<Map<String,Object>> list1 = new ArrayList<>();
        if(getbuspeoplelist.size()>0){
            for (int i=0;i<getbuspeoplelist.size();i++){
                List<Dcount> list = dbusstatiMapper.selectByMonthSize(getbuspeoplelist.get(i));
                for (Dcount d : list) {
                    Map<String,Object> map = new HashMap<>();
                    map.put("日期",d.getName());
                    map.put("商机数量",d.getValue());
                    list1.add(map);
                }
            }
        }else {
            List<Dcount> list = dbusstatiMapper.selectByMonthSize(null);
            for (Dcount d : list) {
                Map<String,Object> map = new HashMap<>();
                map.put("日期",d.getName());
                map.put("商机数量",d.getValue());
                list1.add(map);
            }
        }
        return list1;
    }


    /*折线图我的商机本季度数据*/
    public List<Map<String,Object>> selectByQUARTERMonthSize(String getbuspeople) {
        List<Dcount> list = dbusstatiMapper.selectByQUARTERMonthSize(getbuspeople);
        List<Map<String,Object>> list1 = new ArrayList<>();
        for (Dcount d : list) {
            Map<String,Object> map = new HashMap<>();
            map.put("日期",d.getName());
            map.put("商机数量",d.getValue());
            list1.add(map);
        }
        return list1;
    }


    /*折线图我可查看数据本季度数据*/
    public List<Map<String,Object>> selectByQUARTERMonthSizemyall(HttpServletRequest request) {
        List<String> getbuspeoplelist = dloginService.getbuspeoplelist(request);
        List<Map<String,Object>> list1 = new ArrayList<>();
        if(getbuspeoplelist.size()>0){
            for (int i=0;i<getbuspeoplelist.size();i++){
                List<Dcount> list = dbusstatiMapper.selectByQUARTERMonthSize(getbuspeoplelist.get(i));
                for (Dcount d : list) {
                    Map<String,Object> map = new HashMap<>();
                    map.put("日期",d.getName());
                    map.put("商机数量",d.getValue());
                    list1.add(map);
                }
            }
        }else {
            List<Dcount> list = dbusstatiMapper.selectByQUARTERMonthSize(null);
            for (Dcount d : list) {
                Map<String,Object> map = new HashMap<>();
                map.put("日期",d.getName());
                map.put("商机数量",d.getValue());
                list1.add(map);
            }
        }

        return list1;
    }


    /*折线图过去5年我的商机数据*/
    public List<Map<String,Object>> selectByyearfiveSize(String getbuspeople) {
        List<Dcount> list = dbusstatiMapper.selectByyearfiveSize(getbuspeople);
        List<Map<String,Object>> list1 = new ArrayList<>();
        for (Dcount d : list) {
            Map<String,Object> map = new HashMap<>();
            map.put("日期",d.getName());
            map.put("商机数量",d.getValue());
            list1.add(map);
        }
        return list1;
    }


    /*折线图过去5年我可查看数据*/
    public List<Map<String,Object>> selectByyearfiveSizemyall(HttpServletRequest request) {
        List<String> getbuspeoplelist = dloginService.getbuspeoplelist(request);
        List<Map<String,Object>> list1 = new ArrayList<>();
        if(getbuspeoplelist.size()>0){
            for (int i=0;i<getbuspeoplelist.size();i++){
                List<Dcount> list = dbusstatiMapper.selectByyearfiveSize(getbuspeoplelist.get(i));
                for (Dcount d : list) {
                    Map<String,Object> map = new HashMap<>();
                    map.put("日期",d.getName());
                    map.put("商机数量",d.getValue());
                    list1.add(map);
                }
            }
        }else {
            List<Dcount> list = dbusstatiMapper.selectByyearfiveSize(null);
            for (Dcount d : list) {
                Map<String,Object> map = new HashMap<>();
                map.put("日期",d.getName());
                map.put("商机数量",d.getValue());
                list1.add(map);
            }
        }
        return list1;
    }

    /*漏斗我的商机阶段数据*/
    public List<Dcount> selectByphase(String getbuspeople) {
        return dbusstatiMapper.selectByphase(getbuspeople);
    }


    /*漏斗我可查看商机阶段数据*/
    public List<Dcount> selectByphasemyall(HttpServletRequest request) {
        List<String> getbuspeoplelist = dloginService.getbuspeoplelist(request);
        List<Dcount> list = new ArrayList<>();
        if(getbuspeoplelist.size()>0){
            for (int i=0;i<getbuspeoplelist.size();i++){
                List<Dcount> list2 = dbusstatiMapper.selectByphase(getbuspeoplelist.get(i));
                list.addAll(list2);
            }
        }else{
            List<Dcount> list2 = dbusstatiMapper.selectByphase(null);
            list.addAll(list2);
        }
        return list;
    }

    /*我的商机地图*/
    public List<Dcount> selectBySheng(String getbuspeople) {
        return dbusstatiMapper.selectBySheng(getbuspeople);
    }

    /*我可查看数据地图*/
    public List<Dcount> selectByShengmyall(HttpServletRequest request) {
        List<String> getbuspeoplelist = dloginService.getbuspeoplelist(request);
        List<Dcount> list = new ArrayList<>();
        if(getbuspeoplelist.size()>0){
            for (int i=0;i<getbuspeoplelist.size();i++){
                List<Dcount> list2 = dbusstatiMapper.selectBySheng(getbuspeoplelist.get(i));
                list.addAll(list2);
            }
        }else{
            List<Dcount> list2 = dbusstatiMapper.selectBySheng(null);
            list.addAll(list2);
        }
        return list;
    }


    /*我的商机统计饼状图*/
    public List<DmoneyVo> selectByMoney(String getbuspeople) {
        List<DmoneyVo> strings = dbusstatiMapper.selectByMoney(getbuspeople);
        return strings;
    }


    /*我可查看商机统计饼状图*/
    public List<DmoneyVo> selectByMoneymyall(HttpServletRequest request) {
        List<String> getbuspeoplelist = dloginService.getbuspeoplelist(request);
        List<DmoneyVo> list = new ArrayList<>();
        if(getbuspeoplelist.size()>0){
            for(int i=0;i<getbuspeoplelist.size();i++){
                List<DmoneyVo> dmoneyVos = dbusstatiMapper.selectByMoney(getbuspeoplelist.get(i));
                list.addAll(dmoneyVos);
            }
        }else {
            List<DmoneyVo> dmoneyVos = dbusstatiMapper.selectByMoney(null);
            list.addAll(dmoneyVos);
        }
        return  list;
    }

    /*我的商机表格本季度*/
    public List<Dbusiness> selectByQUARTERData(String getbuspeople) {
        return  dbusstatiMapper.selectByQUARTERData(getbuspeople);
    }

    /*我可查看商机表格本季度*/
    public List<Dbusiness> selectByQUARTERDatamyall(HttpServletRequest request) {
        List<String> getbuspeoplelist = dloginService.getbuspeoplelist(request);
        List<Dbusiness> buslist = new ArrayList<>();
        if(getbuspeoplelist.size()>0){
            for(int i=0;i<getbuspeoplelist.size();i++){
                List<Dbusiness> dbusinesses = dbusstatiMapper.selectByQUARTERData(getbuspeoplelist.get(i));
                buslist.addAll(dbusinesses);
            }
        }else {
            List<Dbusiness> dbusinesses = dbusstatiMapper.selectByQUARTERData(null);
            buslist.addAll(dbusinesses);
        }
        return  buslist;
    }

    /*我的商机本年份的商机*/
    public List<Dbusiness> selectByYearData(String getbuspeople) {
        return dbusstatiMapper.selectByYearData(getbuspeople);
    }

    /*我的商机过去5年的商机*/
    public List<Dbusiness> selectByFiveYearData(String getbuspeople) {
        return dbusstatiMapper.selectByFiveYearData(getbuspeople);
    }

}
